/*      */ package com.waveset.adapter;
/*      */ 
/*      */ import com.sun.idm.logging.trace.Trace;
/*      */ import com.sun.idm.logging.trace.TraceManager;
/*      */ import com.waveset.msgcat.ErrorMessage;
/*      */ import com.waveset.msgcat.Severity;
/*      */ import com.waveset.object.AccountAttributeType;
/*      */ import com.waveset.object.ObjectCache;
/*      */ import com.waveset.object.Resource;
/*      */ import com.waveset.object.ResourceAttribute;
/*      */ import com.waveset.object.Syntax;
/*      */ import com.waveset.util.ConnectionPool;
/*      */ import com.waveset.util.EncryptedData;
/*      */ import com.waveset.util.InternalError;
/*      */ import com.waveset.util.Util;
/*      */ import com.waveset.util.WavesetException;
/*      */ import java.io.PrintStream;
/*      */ import java.sql.Connection;
/*      */ import java.sql.DatabaseMetaData;
/*      */ import java.sql.DriverManager;
/*      */ import java.sql.PreparedStatement;
/*      */ import java.sql.ResultSet;
/*      */ import java.sql.ResultSetMetaData;
/*      */ import java.sql.SQLException;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Collections;
/*      */ import java.util.HashMap;
/*      */ import java.util.HashSet;
/*      */ import java.util.List;
/*      */ import java.util.Set;
/*      */ import java.util.StringTokenizer;
/*      */ 
/*      */ public abstract class JdbcResourceAdapter extends ResourceAdapterBase
/*      */ {
/*      */   public static final String code_id = "$Id: JdbcResourceAdapter.java,v 1.18 2008/11/05 21:55:09 kd229373 Exp $";
/*      */   private static final String CLASS = "com.waveset.adapter.JdbcResourceAdapter";
/*   67 */   private static final Trace TRACE = TraceManager.getTrace("com.waveset.adapter.JdbcResourceAdapter");
/*      */   private boolean _debug;
/*      */   public static final String RA_DRIVER = "JDBC Driver";
/*      */   public static final String RA_URL_PREFIX = "JDBC URL Prefix";
/*      */   public static final String RA_URL_TEMPLATE = "JDBC URL Template";
/*      */   public static final String RA_HOST = "Host";
/*      */   public static final String RA_PORT = "Port";
/*      */   public static final String RA_DATABASE = "Database";
/*      */   public static final String RA_USER = "User";
/*      */   public static final String RA_PASSWORD = "Password";
/*      */   public static final String RA_IDLE_TIMEOUT = "idleTimeout";
/*      */   public static final String RA_QUOTING = "Quoting";
/*      */   public static final String RA_RETHROW_ALL_SQLEXCEPTIONS = "rethrowAllSQLExceptions";
/*      */   public static final String RA_CONNECTION_VALIDATION_SQL = "connectionValidationSQL";
/*      */ 
/*      */   public JdbcResourceAdapter(Resource res, ObjectCache cache)
/*      */   {
/*   84 */     super(res, cache);
/*      */   }
/*      */ 
/*      */   public JdbcResourceAdapter()
/*      */   {
/*      */   }
/*      */ 
/*      */   public void setDebug(boolean b)
/*      */   {
/*   95 */     this._debug = b;
/*      */   }
/*      */ 
/*      */   protected String getResourceAttribute(String name)
/*      */   {
/*  188 */     String METHOD = "getResourceAttribute";
/*  189 */     TRACE.entry2("getResourceAttribute", name);
/*      */ 
/*  191 */     String str = null;
/*  192 */     boolean encrypted = false;
/*      */ 
/*  194 */     if (this._resource != null) {
/*  195 */       ResourceAttribute ra = this._resource.getResourceAttribute(name);
/*  196 */       if (ra != null) {
/*  197 */         Object value = ra.getValue();
/*  198 */         if (value != null)
/*      */         {
/*  200 */           if ((value instanceof EncryptedData)) {
/*  201 */             EncryptedData ed = (EncryptedData)value;
/*  202 */             str = ed.decryptToString();
/*  203 */             encrypted = true;
/*      */           } else {
/*  205 */             str = value.toString();
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*  210 */     if (TRACE.level2("getResourceAttribute")) {
/*  211 */       String val = null;
/*  212 */       if ((str != null) && (encrypted)) {
/*  213 */         StringBuffer sb = new StringBuffer();
/*  214 */         sb.append("<secret> (length=");
/*  215 */         sb.append(Util.length(str));
/*  216 */         sb.append(")");
/*  217 */         val = sb.toString();
/*      */       } else {
/*  219 */         val = str;
/*      */       }
/*  221 */       TRACE.info2("getResourceAttribute", val);
/*      */     }
/*  223 */     TRACE.exit2("getResourceAttribute");
/*  224 */     return str;
/*      */   }
/*      */ 
/*      */   protected String getEncryptedResourceAttribute(String name)
/*      */     throws WavesetException
/*      */   {
/*  237 */     String METHOD = "getEncryptedResourceAttribute";
/*  238 */     TRACE.entry2("getEncryptedResourceAttribute", name);
/*      */ 
/*  240 */     String str = null;
/*      */ 
/*  242 */     if (this._resource != null) {
/*  243 */       ResourceAttribute ra = this._resource.getResourceAttribute(name);
/*  244 */       if (ra != null) {
/*  245 */         Object value = ra.getValue();
/*  246 */         if (value != null)
/*      */         {
/*  248 */           if ((value instanceof EncryptedData)) {
/*  249 */             EncryptedData ed = (EncryptedData)value;
/*  250 */             str = ed.decryptToString();
/*  251 */           } else if ((value instanceof String)) {
/*  252 */             EncryptedData ed = new EncryptedData();
/*      */ 
/*  254 */             ed.fromString((String)value);
/*  255 */             str = ed.decryptToString();
/*      */           } else {
/*  257 */             str = value.toString();
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*  261 */     if (TRACE.level2("getEncryptedResourceAttribute")) {
/*  262 */       StringBuffer sb = new StringBuffer();
/*  263 */       sb.append("<secret> (length=");
/*  264 */       sb.append(Util.length(str));
/*  265 */       sb.append(")");
/*  266 */       TRACE.info2("getEncryptedResourceAttribute", sb.toString());
/*      */     }
/*  268 */     TRACE.exit2("getEncryptedResourceAttribute");
/*  269 */     return str;
/*      */   }
/*      */ 
/*      */   Set getValidSyntaxesFromMapName(String mapName)
/*      */     throws WavesetException
/*      */   {
/*  277 */     String METHOD = "getValidSyntaxesFromMapName";
/*  278 */     TRACE.entry4("getValidSyntaxesFromMapName", mapName);
/*      */ 
/*  280 */     Set syntaxes = new HashSet();
/*      */ 
/*  283 */     AccountAttributeType attrType = getAttrTypeFromMapName(mapName);
/*  284 */     if (attrType != null) {
/*  285 */       syntaxes.add(attrType.getSyntax());
/*      */     }
/*  287 */     TRACE.exit4("getValidSyntaxesFromMapName", syntaxes);
/*      */ 
/*  289 */     return Collections.unmodifiableSet(syntaxes);
/*      */   }
/*      */ 
/*      */   protected boolean attributeRequiresQuotingInSQL(String mapName)
/*      */   {
/*  305 */     String METHOD = "attributeRequiresQuotingInSQL";
/*      */ 
/*  307 */     boolean requiresQuote = true;
/*      */     try
/*      */     {
/*  312 */       Set syntaxes = getValidSyntaxesFromMapName(mapName);
/*  313 */       if (syntaxes.contains(Syntax.INT))
/*  314 */         requiresQuote = false;
/*  315 */       else if ((syntaxes.isEmpty()) && 
/*  316 */         (TRACE.level4("attributeRequiresQuotingInSQL"))) {
/*  317 */         TRACE.info(0L, "attributeRequiresQuotingInSQL", "Assuming that " + mapName + " needs to be quoted " + " since no valid syntax was found.");
/*      */       }
/*      */ 
/*      */     }
/*      */     catch (WavesetException e)
/*      */     {
/*  323 */       TRACE.caught2("attributeRequiresQuotingInSQL", e);
/*  324 */       TRACE.info2("attributeRequiresQuotingInSQL", "Assuming that " + mapName + " needs to be quoted since " + " an exception was caught: " + e);
/*      */     }
/*      */ 
/*  329 */     if (TRACE.level4("attributeRequiresQuotingInSQL")) {
/*  330 */       TRACE.info(0L, "attributeRequiresQuotingInSQL", "Mapped attribute " + mapName + " does " + (requiresQuote ? "" : "not ") + " require quoting.");
/*      */     }
/*      */ 
/*  335 */     return requiresQuote;
/*      */   }
/*      */ 
/*      */   public void addAttributeValue(StringBuffer b, String attrValue, String attrName)
/*      */   {
/*  344 */     if (attributeRequiresQuotingInSQL(attrName))
/*  345 */       addQuoted(b, attrValue);
/*      */     else
/*  347 */       b.append(attrValue);
/*      */   }
/*      */ 
/*      */   public void addQuoted(StringBuffer b, String s)
/*      */   {
/*  358 */     b.append("'");
/*  359 */     if (s != null) {
/*  360 */       for (int i = 0; i < s.length(); i++) {
/*  361 */         char ch = s.charAt(i);
/*  362 */         if (ch == '\'')
/*  363 */           b.append("''");
/*      */         else
/*  365 */           b.append(ch);
/*      */       }
/*      */     }
/*  368 */     b.append("'");
/*      */   }
/*      */ 
/*      */   protected List decodeCommaList(String src)
/*      */   {
/*  376 */     ArrayList l = null;
/*  377 */     if (src != null) {
/*  378 */       StringTokenizer st = new StringTokenizer(src, ",");
/*  379 */       while (st.hasMoreElements()) {
/*  380 */         if (l == null) {
/*  381 */           l = new ArrayList();
/*      */         }
/*  383 */         l.add(st.nextToken().trim());
/*      */       }
/*      */     }
/*      */ 
/*  387 */     return l;
/*      */   }
/*      */ 
/*      */   protected String encodeCommaList(List list)
/*      */   {
/*  395 */     String s = null;
/*  396 */     if (list != null) {
/*  397 */       StringBuffer b = new StringBuffer();
/*  398 */       for (int i = 0; i < list.size(); i++) {
/*  399 */         String role = (String)list.get(i);
/*  400 */         if (i > 0)
/*  401 */           b.append(", ");
/*  402 */         b.append(role);
/*      */       }
/*  404 */       s = b.toString();
/*      */     }
/*  406 */     return s;
/*      */   }
/*      */ 
/*      */   protected List mergeCommaLists(String s1, String s2)
/*      */   {
/*  417 */     List merge = null;
/*      */ 
/*  420 */     if (s1 != null) {
/*  421 */       merge = decodeCommaList(s1);
/*      */     }
/*      */ 
/*  424 */     List l2 = decodeCommaList(s2);
/*      */ 
/*  426 */     return mergeStringLists(merge, l2);
/*      */   }
/*      */ 
/*      */   protected List mergeStringLists(List l1, List l2)
/*      */   {
/*  438 */     List merge = null;
/*      */ 
/*  440 */     if (l1 != null) {
/*  441 */       merge = l1;
/*      */     }
/*  443 */     if (merge == null) {
/*  444 */       merge = l2;
/*  445 */     } else if (l2 != null) {
/*  446 */       int max = l2.size();
/*  447 */       for (int i = 0; i < max; i++) {
/*  448 */         String name = (String)l2.get(i);
/*      */ 
/*  451 */         if (!merge.contains(name)) {
/*  452 */           merge.add(name);
/*      */         }
/*      */       }
/*      */     }
/*  456 */     return merge;
/*      */   }
/*      */ 
/*      */   public Connection getConnection()
/*      */     throws WavesetException
/*      */   {
/*  470 */     return getConnection(null);
/*      */   }
/*      */ 
/*      */   protected Connection getConnection(String database)
/*      */     throws WavesetException
/*      */   {
/*  478 */     String url = formatUrl(database);
/*  479 */     Connection conn = getConnectionUrl(url);
/*  480 */     return conn;
/*      */   }
/*      */ 
/*      */   protected String formatUrl(String database)
/*      */     throws WavesetException
/*      */   {
/*  488 */     String METHOD = "formatUrl";
/*  489 */     TRACE.entry2("formatUrl");
/*  490 */     if (TRACE.level2("formatUrl")) {
/*  491 */       StringBuffer sb = new StringBuffer(48);
/*  492 */       sb.append("database=");
/*  493 */       sb.append(database);
/*  494 */       TRACE.info2("formatUrl", sb.toString());
/*      */     }
/*      */ 
/*  497 */     String url = null;
/*      */ 
/*  499 */     if (database == null) {
/*  500 */       database = getResourceAttribute("Database");
/*  501 */       TRACE.variable2("formatUrl", "database=", database);
/*      */     }
/*      */ 
/*  506 */     String template = getResourceAttribute("JDBC URL Template");
/*  507 */     if (template != null) {
/*  508 */       template = template.trim();
/*  509 */       if (template.length() > 0) {
/*  510 */         url = formatTemplateUrl(template, database);
/*      */       }
/*      */ 
/*      */     }
/*      */ 
/*  516 */     if (url == null) {
/*  517 */       String prefix = getResourceAttribute("JDBC URL Prefix");
/*  518 */       String host = getResourceAttribute("Host");
/*  519 */       String port = getResourceAttribute("Port");
/*      */ 
/*  521 */       if ((prefix == null) || (host == null) || (port == null)) {
/*  522 */         String msg = "Insufficient connect URL parameters.";
/*  523 */         WavesetException we = new WavesetException("Insufficient connect URL parameters.", Severity.ERROR);
/*  524 */         if (TRACE.level3("formatUrl")) {
/*  525 */           if (prefix == null) {
/*  526 */             TRACE.info(0L, "formatUrl", "prefix == null");
/*      */           }
/*  528 */           if (host == null) {
/*  529 */             TRACE.info(0L, "formatUrl", "host == null");
/*      */           }
/*  531 */           if (port == null) {
/*  532 */             TRACE.info(0L, "formatUrl", "port == null");
/*      */           }
/*      */         }
/*  535 */         TRACE.throwing2("formatUrl", we);
/*  536 */         throw we;
/*      */       }
/*  538 */       if (TRACE.level3("formatUrl")) {
/*  539 */         StringBuffer sb = new StringBuffer(128);
/*  540 */         sb.append("\nprefix=");
/*  541 */         sb.append(prefix);
/*  542 */         sb.append("\nhost=");
/*  543 */         sb.append(host);
/*  544 */         sb.append("\nport=");
/*  545 */         sb.append(port);
/*  546 */         TRACE.info(0L, "formatUrl", sb.toString());
/*      */       }
/*      */ 
/*  549 */       url = formatUrl(prefix, host, port, database);
/*      */     }
/*      */ 
/*  552 */     TRACE.exit2("formatUrl", url);
/*  553 */     return url;
/*      */   }
/*      */ 
/*      */   public String formatUrl(String prefix, String host, String port, String db)
/*      */   {
/*  564 */     return prefix + "://" + host + ":" + port + "/" + db;
/*      */   }
/*      */ 
/*      */   public String formatTemplateUrl(String template, String database)
/*      */   {
/*  573 */     String METHOD = "formatTemplateUrl";
/*  574 */     TRACE.entry1("formatTemplateUrl");
/*  575 */     if (TRACE.level1("formatTemplateUrl")) {
/*  576 */       StringBuffer sb = new StringBuffer(48);
/*  577 */       sb.append("template=");
/*  578 */       sb.append(template);
/*  579 */       sb.append(", database=");
/*  580 */       sb.append(database);
/*  581 */       TRACE.info1("formatTemplateUrl", sb.toString());
/*      */     }
/*      */ 
/*  584 */     StringBuffer b = new StringBuffer();
/*      */ 
/*  586 */     int len = template.length();
/*  587 */     for (int i = 0; i < len; i++) {
/*  588 */       char ch = template.charAt(i);
/*  589 */       if (ch != '%') {
/*  590 */         b.append(ch);
/*  591 */       } else if (i + 1 < len) {
/*  592 */         i++;
/*  593 */         ch = template.charAt(i);
/*  594 */         if (ch == '%')
/*  595 */           b.append(ch);
/*  596 */         else if (ch == 'h')
/*  597 */           b.append(getResourceAttribute("Host"));
/*  598 */         else if (ch == 'p')
/*  599 */           b.append(getResourceAttribute("Port"));
/*  600 */         else if (ch == 'd')
/*  601 */           b.append(database);
/*      */       }
/*      */     }
/*  604 */     String formattedURL = b.toString();
/*      */ 
/*  606 */     TRACE.exit1("formatTemplateUrl", formattedURL);
/*  607 */     return formattedURL;
/*      */   }
/*      */ 
/*      */   protected Connection getConnectionUrl(String url)
/*      */     throws WavesetException
/*      */   {
/*  615 */     String METHOD = "getConnectionUrl";
/*  616 */     TRACE.entry2("getConnectionUrl");
/*  617 */     if (TRACE.level2("getConnectionUrl")) {
/*  618 */       StringBuffer sb = new StringBuffer(48);
/*  619 */       sb.append("url=");
/*  620 */       sb.append(url);
/*  621 */       TRACE.info2("getConnectionUrl", sb.toString());
/*      */     }
/*      */ 
/*  624 */     String driver = getResourceAttribute("JDBC Driver");
/*  625 */     String prefix = getResourceAttribute("JDBC URL Prefix");
/*  626 */     String name = getResourceAttribute("User");
/*  627 */     String pass = getEncryptedResourceAttribute("Password");
/*      */ 
/*  630 */     Resource resource = getResource();
/*  631 */     String validationSql = null;
/*  632 */     if (resource != null) {
/*  633 */       validationSql = (String)resource.getResourceAttributeVal("connectionValidationSQL");
/*      */     }
/*      */ 
/*  637 */     long idleTimeout = -1L;
/*      */ 
/*  639 */     String idleTimeoutStr = getResourceAttribute("idleTimeout");
/*  640 */     if (idleTimeoutStr != null) {
/*      */       try {
/*  642 */         idleTimeout = Long.parseLong(idleTimeoutStr);
/*      */       }
/*      */       catch (NumberFormatException nfe) {
/*  645 */         WavesetException we = new WavesetException(nfe);
/*  646 */         TRACE.throwing2("getConnectionUrl", we);
/*  647 */         throw we;
/*      */       }
/*      */     }
/*      */ 
/*  651 */     ConnectionPool pool = ConnectionPool.getConnectionPool();
/*  652 */     if (TRACE.level3("getConnectionUrl")) {
/*  653 */       StringBuffer sb = new StringBuffer(128);
/*  654 */       sb.append("driver=");
/*  655 */       sb.append(driver);
/*  656 */       sb.append(", prefix=");
/*  657 */       sb.append(prefix);
/*  658 */       sb.append(", name=");
/*  659 */       sb.append(name);
/*  660 */       sb.append(", pass=");
/*  661 */       StringBuffer b = null;
/*  662 */       if (pass != null) {
/*  663 */         b = new StringBuffer(pass);
/*  664 */         b.replace(0, pass.length(), "*");
/*      */       }
/*  666 */       sb.append(b);
/*  667 */       if (validationSql != null) {
/*  668 */         sb.append(", ");
/*  669 */         sb.append("connectionValidationSQL");
/*  670 */         sb.append(" = '");
/*  671 */         sb.append(validationSql);
/*  672 */         sb.append("'");
/*      */       }
/*  674 */       sb.append(", idleTimeout=" + idleTimeout);
/*  675 */       TRACE.info(0L, "getConnectionUrl", sb.toString());
/*      */     }
/*  677 */     Connection con = pool.getConnection(driver, prefix, url, name, pass, validationSql, idleTimeout);
/*  678 */     TRACE.exit2("getConnectionUrl", getConnectionString(con));
/*  679 */     return con;
/*      */   }
/*      */ 
/*      */   protected Connection getConnectionUrl(HashMap args)
/*      */     throws WavesetException
/*      */   {
/*  687 */     String METHOD = "getConnectionUrl(HashMap)";
/*  688 */     TRACE.entry2("getConnectionUrl(HashMap)");
/*  689 */     if (TRACE.level2("getConnectionUrl(HashMap)")) {
/*  690 */       TRACE.info2("getConnectionUrl(HashMap)", "args=" + args);
/*      */     }
/*      */ 
/*  693 */     String driver = (String)args.get("driver");
/*  694 */     String prefix = (String)args.get("JDBC URL Prefix");
/*  695 */     String name = (String)args.get("user");
/*  696 */     String pass = (String)args.get("password");
/*  697 */     String url = (String)args.get("url");
/*      */ 
/*  699 */     ConnectionPool pool = ConnectionPool.getConnectionPool();
/*      */ 
/*  702 */     Resource resource = getResource();
/*  703 */     String validationSql = null;
/*  704 */     if (resource != null) {
/*  705 */       validationSql = (String)resource.getResourceAttributeVal("connectionValidationSQL");
/*      */     }
/*      */ 
/*  709 */     long idleTimeout = -1L;
/*      */ 
/*  711 */     String idleTimeoutStr = getResourceAttribute("idleTimeout");
/*  712 */     if (idleTimeoutStr != null) {
/*      */       try {
/*  714 */         idleTimeout = Long.parseLong(idleTimeoutStr);
/*      */       }
/*      */       catch (NumberFormatException nfe) {
/*  717 */         WavesetException we = new WavesetException(nfe);
/*  718 */         TRACE.throwing2("getConnectionUrl(HashMap)", we);
/*  719 */         throw we;
/*      */       }
/*      */     }
/*      */ 
/*  723 */     if (TRACE.level3("getConnectionUrl(HashMap)")) {
/*  724 */       StringBuffer sb = new StringBuffer(196);
/*  725 */       sb.append("driver=");
/*  726 */       sb.append(driver);
/*  727 */       sb.append(", prefix=");
/*  728 */       sb.append(prefix);
/*  729 */       sb.append(", name=");
/*  730 */       sb.append(name);
/*  731 */       sb.append(", pass=");
/*  732 */       StringBuffer b = null;
/*  733 */       if (pass != null) {
/*  734 */         b = new StringBuffer(pass);
/*  735 */         b.replace(0, pass.length(), "*");
/*      */       }
/*  737 */       sb.append(b);
/*  738 */       sb.append(", url=");
/*  739 */       sb.append(url);
/*  740 */       if (validationSql != null) {
/*  741 */         sb.append(", ");
/*  742 */         sb.append("connectionValidationSQL");
/*  743 */         sb.append(" = '");
/*  744 */         sb.append(validationSql);
/*  745 */         sb.append("'");
/*      */       }
/*  747 */       sb.append(", idleTimeout=" + idleTimeout);
/*  748 */       TRACE.info(0L, "getConnectionUrl(HashMap)", sb.toString());
/*      */     }
/*      */ 
/*  751 */     Connection con = pool.getConnection(driver, prefix, url, name, pass, validationSql, idleTimeout);
/*  752 */     TRACE.exit2("getConnectionUrl(HashMap)", getConnectionString(con));
/*  753 */     return con;
/*      */   }
/*      */ 
/*      */   protected Connection getConnection(String db, String user, String password)
/*      */     throws WavesetException
/*      */   {
/*  764 */     String METHOD = "getConnection";
/*  765 */     TRACE.entry2("getConnection");
/*  766 */     if (TRACE.level2("getConnection")) {
/*  767 */       StringBuffer sb = new StringBuffer(48);
/*  768 */       sb.append("db=");
/*  769 */       sb.append(db);
/*  770 */       sb.append(", user=");
/*  771 */       sb.append(user);
/*  772 */       sb.append(", password=");
/*  773 */       StringBuffer b = null;
/*  774 */       if (password != null) {
/*  775 */         b = new StringBuffer(password);
/*  776 */         b.replace(0, password.length(), "*");
/*      */       }
/*  778 */       sb.append(b);
/*  779 */       TRACE.info2("getConnection", sb.toString());
/*      */     }
/*  781 */     Connection con = null;
/*      */ 
/*  785 */     if (this._resource == null) {
/*  786 */       String msg = "JdbcResourceAdapter: no Resource";
/*  787 */       InternalError ie = new InternalError("JdbcResourceAdapter: no Resource");
/*  788 */       TRACE.throwing2("getConnection", ie);
/*  789 */       throw ie;
/*      */     }
/*      */ 
/*  792 */     if ((user == null) || (password == null)) {
/*  793 */       String message = "JdbcResourceAdapter: insufficient connection credentials.";
/*      */ 
/*  795 */       ErrorMessage msg = new ErrorMessage(Severity.ERROR, "JdbcResourceAdapter: insufficient connection credentials.");
/*  796 */       WavesetException we = new WavesetException(msg);
/*  797 */       TRACE.throwing2("getConnection", we);
/*  798 */       throw we;
/*      */     }
/*      */ 
/*  801 */     String driver = getResourceAttribute("JDBC Driver");
/*  802 */     if (driver == null) {
/*  803 */       String message = "JdbcResourceAdapter: No driver specified.";
/*  804 */       ErrorMessage msg = new ErrorMessage(Severity.ERROR, "JdbcResourceAdapter: No driver specified.");
/*  805 */       WavesetException we = new WavesetException(msg);
/*  806 */       TRACE.throwing2("getConnection", we);
/*  807 */       throw we;
/*      */     }
/*      */ 
/*  812 */     String prefix = getResourceAttribute("JDBC URL Prefix");
/*      */ 
/*  814 */     ConnectionPool.registerDriver(driver, prefix);
/*      */ 
/*  816 */     String url = formatUrl(db);
/*      */     try
/*      */     {
/*  819 */       con = DriverManager.getConnection(url, user, password);
/*      */     } catch (SQLException e) {
/*  821 */       TRACE.throwing2("getConnection", e);
/*  822 */       throw new WavesetException(e);
/*      */     }
/*  824 */     TRACE.exit2("getConnection", getConnectionString(con));
/*  825 */     return con;
/*      */   }
/*      */ 
/*      */   public void println(String msg)
/*      */   {
/*  835 */     System.out.println(msg);
/*      */   }
/*      */ 
/*      */   public void closeConnection(Connection con)
/*      */   {
/*  843 */     String METHOD = "closeConnection";
/*  844 */     TRACE.entry1("closeConnection");
/*  845 */     if (TRACE.level1("closeConnection")) {
/*  846 */       StringBuffer sb = new StringBuffer(48);
/*  847 */       sb.append("connection=");
/*  848 */       sb.append(getConnectionString(con));
/*  849 */       TRACE.info1("closeConnection", sb.toString());
/*      */     }
/*      */ 
/*  852 */     if (con != null) {
/*      */       try {
/*  854 */         con.close();
/*      */       } catch (SQLException e) {
/*  856 */         if (TRACE.level3("closeConnection")) {
/*  857 */           StringBuffer sb = new StringBuffer();
/*  858 */           sb.append("WARNING: Exception closing JDBC connection\n");
/*  859 */           sb.append(e.getMessage());
/*  860 */           TRACE.info(0L, "closeConnection", sb.toString());
/*      */         }
/*      */       }
/*      */     }
/*  864 */     TRACE.exit1("closeConnection");
/*      */   }
/*      */ 
/*      */   public void closeStatement(PreparedStatement stmt)
/*      */   {
/*  872 */     String METHOD = "closeStatement";
/*  873 */     TRACE.entry1("closeStatement");
/*  874 */     if (TRACE.level1("closeStatement")) {
/*  875 */       TRACE.info1("closeStatement", "stmt=" + stmt);
/*      */     }
/*      */ 
/*  878 */     if (stmt != null) {
/*      */       try
/*      */       {
/*  881 */         stmt.close();
/*      */       } catch (SQLException e) {
/*  883 */         StringBuffer sb = new StringBuffer(64);
/*  884 */         sb.append("WARNING: Exception closing JDBC statement: ");
/*  885 */         sb.append(e.getMessage());
/*  886 */         TRACE.info2("closeStatement", sb.toString());
/*      */       }
/*      */     }
/*      */ 
/*  890 */     TRACE.exit1("closeStatement");
/*      */   }
/*      */ 
/*      */   public void closeResult(ResultSet res)
/*      */   {
/*  898 */     String METHOD = "closeResult";
/*  899 */     TRACE.entry1("closeResult");
/*      */ 
/*  901 */     if (res != null) {
/*      */       try {
/*  903 */         res.close();
/*      */       } catch (SQLException e) {
/*  905 */         StringBuffer sb = new StringBuffer("WARNING: Exception closing JDBC result\n");
/*      */ 
/*  907 */         sb.append(e.getMessage());
/*  908 */         TRACE.info2("closeResult", sb.toString());
/*      */       }
/*      */     }
/*      */ 
/*  912 */     TRACE.exit1("closeResult");
/*      */   }
/*      */ 
/*      */   public int queryInt(Connection c, String q, String arg)
/*      */     throws WavesetException
/*      */   {
/*  922 */     String METHOD = "queryInt";
/*  923 */     TRACE.entry1("queryInt");
/*  924 */     if (TRACE.level1("queryInt")) {
/*  925 */       StringBuffer sb = new StringBuffer(64);
/*  926 */       sb.append("\nconnection=");
/*  927 */       sb.append(getConnectionString(c));
/*  928 */       sb.append("\nquery=");
/*  929 */       sb.append(q);
/*  930 */       sb.append("\nargument=");
/*  931 */       sb.append(arg);
/*  932 */       TRACE.info1("queryInt", sb.toString());
/*      */     }
/*      */ 
/*  935 */     int value = 0;
/*      */     try
/*      */     {
/*  938 */       PreparedStatement s = c.prepareStatement(q);
/*  939 */       ResultSet rs = null;
/*      */       try
/*      */       {
/*  942 */         if (arg != null)
/*  943 */           s.setString(1, arg);
/*  944 */         rs = s.executeQuery();
/*  945 */         if (rs.next()) {
/*  946 */           value = rs.getInt(1);
/*      */ 
/*  948 */           if (rs.wasNull())
/*  949 */             value = 0;
/*      */         }
/*      */       } finally {
/*  952 */         if (rs != null)
/*  953 */           rs.close();
/*  954 */         s.close();
/*      */       }
/*      */     } catch (SQLException e) {
/*  957 */       TRACE.throwing1("queryInt", e);
/*  958 */       throw new InternalError(e);
/*      */     }
/*      */ 
/*  961 */     TRACE.exit1("queryInt", value);
/*  962 */     return value;
/*      */   }
/*      */ 
/*      */   public String queryString(Connection c, String q, String arg)
/*      */     throws WavesetException
/*      */   {
/*  972 */     String METHOD = "queryString";
/*  973 */     TRACE.entry1("queryString");
/*  974 */     if (TRACE.level1("queryString")) {
/*  975 */       StringBuffer sb = new StringBuffer(64);
/*  976 */       sb.append("\nconnection=");
/*  977 */       sb.append(getConnectionString(c));
/*  978 */       sb.append("\nquery=");
/*  979 */       sb.append(q);
/*  980 */       sb.append("\nargument=");
/*  981 */       sb.append(arg);
/*  982 */       TRACE.info1("queryString", sb.toString());
/*      */     }
/*      */ 
/*  986 */     String value = null;
/*      */     try
/*      */     {
/*  989 */       PreparedStatement s = c.prepareStatement(q);
/*  990 */       ResultSet rs = null;
/*      */       try
/*      */       {
/*  993 */         if (arg != null)
/*  994 */           s.setString(1, arg);
/*  995 */         rs = s.executeQuery();
/*  996 */         if (rs.next())
/*  997 */           value = rs.getString(1);
/*      */       }
/*      */       finally {
/* 1000 */         if (rs != null)
/* 1001 */           rs.close();
/* 1002 */         s.close();
/*      */       }
/*      */     } catch (SQLException e) {
/* 1005 */       TRACE.throwing1("queryString", e);
/* 1006 */       throw new InternalError(e);
/*      */     }
/*      */ 
/* 1009 */     TRACE.exit1("queryString", value);
/* 1010 */     return value;
/*      */   }
/*      */ 
/*      */   protected void execProcedure(Connection con, String stmt)
/*      */     throws WavesetException
/*      */   {
/* 1019 */     execProcedure(con, stmt, 0);
/*      */   }
/*      */ 
/*      */   protected void execProcedure(Connection con, String stmt, int ignoreError)
/*      */     throws WavesetException
/*      */   {
/* 1029 */     String METHOD = "execProcedure";
/* 1030 */     TRACE.entry2("execProcedure");
/* 1031 */     if (TRACE.level2("execProcedure")) {
/* 1032 */       StringBuffer sb = new StringBuffer(96);
/* 1033 */       sb.append("\nconnection=");
/* 1034 */       sb.append(getConnectionString(con));
/* 1035 */       sb.append("\nstatement=");
/* 1036 */       sb.append(stmt);
/* 1037 */       sb.append("\nignoreError=");
/* 1038 */       sb.append(ignoreError);
/* 1039 */       TRACE.info2("execProcedure", sb.toString());
/*      */     }
/*      */ 
/* 1042 */     PreparedStatement s = null;
/*      */     try
/*      */     {
/* 1045 */       s = con.prepareStatement(stmt);
/* 1046 */       s.execute();
/*      */ 
/* 1050 */       flushResults(stmt, s);
/*      */     }
/*      */     catch (SQLException e) {
/* 1053 */       Resource resource = getResource();
/* 1054 */       Object value = null;
/* 1055 */       if (resource != null) {
/* 1056 */         value = resource.getResourceAttributeVal("rethrowAllSQLExceptions");
/*      */       }
/* 1058 */       boolean throwIt = (value != null) && (((Boolean)value).booleanValue());
/*      */ 
/* 1060 */       int code = e.getErrorCode();
/* 1061 */       if ((throwIt) || (code != ignoreError)) {
/* 1062 */         TRACE.throwing2("execProcedure", e);
/* 1063 */         throw new InternalError(e);
/*      */       }
/* 1065 */       if (TRACE.level3("execProcedure"))
/* 1066 */         TRACE.info(0L, "execProcedure", e.getMessage());
/*      */     }
/*      */     finally
/*      */     {
/* 1070 */       closeStatement(s);
/*      */     }
/* 1072 */     TRACE.exit2("execProcedure");
/*      */   }
/*      */ 
/*      */   protected void execSql(Connection con, String stmt)
/*      */     throws WavesetException
/*      */   {
/* 1080 */     execSql(con, stmt, null, 0);
/*      */   }
/*      */ 
/*      */   protected void execSql(Connection con, String stmt, String arg)
/*      */     throws WavesetException
/*      */   {
/* 1089 */     execSql(con, stmt, arg, 0);
/*      */   }
/*      */ 
/*      */   protected void execSql(Connection con, String stmt, String arg, int ignoreError)
/*      */     throws WavesetException
/*      */   {
/* 1102 */     String METHOD = "execSql";
/* 1103 */     TRACE.entry2("execSql");
/* 1104 */     if (TRACE.level2("execSql")) {
/* 1105 */       StringBuffer sb = new StringBuffer(64);
/* 1106 */       sb.append("\nconnection=");
/* 1107 */       sb.append(getConnectionString(con));
/* 1108 */       sb.append("\nstatement=");
/* 1109 */       sb.append(stmt);
/* 1110 */       sb.append("\narg=");
/* 1111 */       sb.append(arg);
/* 1112 */       sb.append("\nignoreError=");
/* 1113 */       sb.append(ignoreError);
/* 1114 */       TRACE.info2("execSql", sb.toString());
/*      */     }
/*      */ 
/* 1117 */     PreparedStatement s = null;
/*      */     try
/*      */     {
/* 1120 */       s = con.prepareStatement(stmt);
/* 1121 */       if (arg != null)
/* 1122 */         s.setString(1, arg);
/* 1123 */       s.execute();
/* 1124 */       flushResults(stmt, s);
/*      */     }
/*      */     catch (SQLException e) {
/* 1127 */       Resource resource = getResource();
/* 1128 */       Object value = null;
/* 1129 */       if (resource != null) {
/* 1130 */         value = resource.getResourceAttributeVal("rethrowAllSQLExceptions");
/*      */       }
/* 1132 */       boolean throwIt = (value != null) && (((Boolean)value).booleanValue());
/*      */ 
/* 1134 */       int code = e.getErrorCode();
/* 1135 */       if ((throwIt) || (code != ignoreError)) {
/* 1136 */         TRACE.throwing2("execSql", e);
/* 1137 */         throw new InternalError(e);
/*      */       }
/* 1139 */       if (TRACE.level3("execSql"))
/* 1140 */         TRACE.info(0L, "execSql", e.getMessage());
/*      */     }
/*      */     finally
/*      */     {
/* 1144 */       closeStatement(s);
/*      */     }
/* 1146 */     TRACE.exit2("execSql");
/*      */   }
/*      */ 
/*      */   protected void flushResults(String stmt, PreparedStatement st)
/*      */     throws SQLException
/*      */   {
/* 1168 */     String METHOD = "flushResults";
/* 1169 */     TRACE.entry2("flushResults");
/* 1170 */     if (TRACE.level2("flushResults")) {
/* 1171 */       StringBuffer sb = new StringBuffer(64);
/* 1172 */       sb.append("\nstatement=");
/* 1173 */       sb.append(stmt);
/* 1174 */       sb.append("\nprepared statement");
/* 1175 */       if (st == null) {
/* 1176 */         sb.append(" was null");
/*      */       } else {
/* 1178 */         sb.append("=");
/* 1179 */         sb.append(st);
/*      */       }
/* 1181 */       TRACE.info2("flushResults", sb.toString());
/*      */     }
/*      */ 
/* 1184 */     if (this._debug) {
/* 1185 */       println("Flushing results for: " + stmt);
/*      */     }
/* 1187 */     int result = 1;
/* 1188 */     boolean more = st != null;
/* 1189 */     while (more)
/*      */     {
/* 1192 */       int rowCount = st.getUpdateCount();
/* 1193 */       if (rowCount < 0)
/*      */       {
/* 1199 */         ResultSet rs = st.getResultSet();
/* 1200 */         if (rs != null) {
/* 1201 */           if (this._debug) {
/* 1202 */             println("ResultSet " + Util.itoa(result));
/* 1203 */             dumpResult(rs);
/*      */           }
/* 1205 */           rs.close();
/*      */         }
/*      */         else {
/* 1208 */           more = false;
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1213 */       if (more) {
/* 1214 */         more = st.getMoreResults();
/*      */       }
/* 1216 */       result++;
/*      */     }
/* 1218 */     TRACE.exit2("flushResults");
/*      */   }
/*      */ 
/*      */   public void dumpResult(ResultSet rs)
/*      */     throws SQLException
/*      */   {
/* 1226 */     ResultSetMetaData md = rs.getMetaData();
/* 1227 */     int cols = md.getColumnCount();
/*      */ 
/* 1229 */     StringBuffer b = new StringBuffer();
/* 1230 */     for (int i = 0; i < cols; i++) {
/* 1231 */       if (i > 0)
/* 1232 */         b.append(", ");
/* 1233 */       b.append(md.getColumnLabel(i));
/* 1234 */       b.append(" (");
/* 1235 */       b.append(md.getColumnTypeName(i));
/* 1236 */       b.append(") ");
/*      */     }
/* 1238 */     println(b.toString());
/*      */ 
/* 1240 */     while (rs.next()) {
/* 1241 */       b.setLength(0);
/* 1242 */       for (int i = 0; i < cols; i++) {
/* 1243 */         if (i > 0)
/* 1244 */           b.append(", ");
/* 1245 */         Object o = rs.getObject(i);
/* 1246 */         if (o == null)
/* 1247 */           b.append("null");
/*      */         else
/* 1249 */           b.append(o.toString());
/*      */       }
/* 1251 */       println(b.toString());
/*      */     }
/*      */   }
/*      */ 
/*      */   protected void flushResultsQuietly(PreparedStatement st)
/*      */   {
/* 1261 */     String METHOD = "flushResultsQuietly";
/* 1262 */     TRACE.entry2("flushResultsQuietly");
/* 1263 */     if (TRACE.level2("flushResultsQuietly")) {
/* 1264 */       StringBuffer sb = new StringBuffer("statement=");
/* 1265 */       sb.append(st);
/* 1266 */       TRACE.info2("flushResultsQuietly", sb.toString());
/*      */     }
/*      */     try
/*      */     {
/* 1270 */       flushResults(null, st);
/*      */     } catch (Throwable t) {
/* 1272 */       if (TRACE.level3("flushResultsQuietly")) {
/* 1273 */         TRACE.info(0L, "flushResultsQuietly", t.getMessage());
/*      */       }
/*      */     }
/* 1276 */     TRACE.exit2("flushResultsQuietly");
/*      */   }
/*      */ 
/*      */   protected String getConnectionString(Connection c)
/*      */   {
/* 1283 */     String connInfo = null;
/* 1284 */     if (c != null)
/*      */       try {
/* 1286 */         connInfo = c.getMetaData().getURL();
/*      */       }
/*      */       catch (SQLException e) {
/*      */       }
/*      */     else {
/* 1291 */       connInfo = null;
/*      */     }
/* 1293 */     return connInfo;
/*      */   }
/*      */ }

/* Location:           E:\IDM_Code\idmadapter.jar
 * Qualified Name:     com.waveset.adapter.JdbcResourceAdapter
 * JD-Core Version:    0.6.0
 */